package com.yc.sole.erp.biz;

import cn.hutool.core.bean.BeanUtil;
import com.yc.sole.erp.dto.EmployeeDto;
import com.yc.sole.erp.dto.ProcessDto;
import com.yc.sole.erp.dto.ProcessEmployeeDto;
import com.yc.sole.erp.repository.EmployeeRepository;
import com.yc.sole.erp.repository.ProcessEmployeeRepository;
import com.yc.sole.erp.repository.ProcessRepository;
import com.yc.sole.erp.request.ProcessEmployeeReq;
import com.yc.sole.erp.request.ProcessQueryReq;
import com.yc.sole.erp.request.ProcessReq;
import com.yc.sole.erp.response.EmployeeResp;
import com.yc.sole.erp.response.ProcessResp;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2024-03-06
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class ProcessBiz {

    private final ProcessRepository processRepository;
    private final ProcessEmployeeRepository processEmployeeRepository;
    private final EmployeeRepository employeeRepository;

    /**
     * 新增
     *
     * @param processReq
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveProcess(ProcessReq processReq) {
        ProcessDto dto = new ProcessDto();
        BeanUtil.copyProperties(processReq, dto);
        processRepository.saveProcess(dto);
    }

    /**
     * 修改
     *
     * @param processReq
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateProcessById(ProcessReq processReq) {
        ProcessDto dto = new ProcessDto();
        BeanUtil.copyProperties(processReq, dto);
        processRepository.updateProcessById(dto);
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteProcessByIds(Collection<Long> ids) {
        processRepository.deleteProcessByIds(ids);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public ProcessResp selectProcessById(Long id) {
        return BeanUtil.copyProperties(processRepository.selectProcessById(id), ProcessResp.class);
    }

    /**
     * 查询所有可用工序
     *
     * @return
     */
    public List<ProcessResp> selectAllProcess() {
        return BeanUtil.copyToList(processRepository.selectAllProcess(), ProcessResp.class);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param processQueryReq
     * @return
     */
    public Paging<ProcessResp> selectProcessPage(Paging<ProcessQueryReq> page, ProcessQueryReq processQueryReq) {
        Paging<ProcessDto> paging = processRepository.selectProcessPage(page, processQueryReq);
        return PageUtil.pageCopy(paging, ProcessResp.class);
    }

    /**
     * 根据工序id查询员工
     *
     * @param processId
     * @return
     */
    public List<EmployeeResp> selectEmployeeByProcessId(Long processId) {
        Set<Long> employeeIds = processEmployeeRepository.selectEmployeeIdByProcessId(processId);
        List<EmployeeDto> employeeDtoList = employeeRepository.selectEmployeeByIds(employeeIds);
        return BeanUtil.copyToList(employeeDtoList, EmployeeResp.class);
    }

    /**
     * 修改工序关联员工
     *
     * @param req
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateProcessEmployee(ProcessEmployeeReq req) {
        // 删除
        processEmployeeRepository.deleteByProcessId(req.getProcessId());
        // 新增
        List<ProcessEmployeeDto> processEmployeeDtoList = req.getEmployeeIds().stream().map(employeeId -> {
            ProcessEmployeeDto processEmployeeDto = new ProcessEmployeeDto();
            processEmployeeDto.setEmployeeId(employeeId);
            processEmployeeDto.setProcessId(req.getProcessId());
            return processEmployeeDto;
        }).toList();
        processEmployeeRepository.saveProcessEmployee(processEmployeeDtoList);
    }
}
